export default {
    data() {
        return {
			userId:'',
            isShowQrCode: false, // 是否显示公众号二维码
            role: 0, //当前设定的角色： 1为消费者，2为上货员
            isManage: false, // 当前用户是否为上货员
            contactPhone: "--", // 联系电话
            isshowSub: true
        }
    },
    onLoad() {
        this.$util.checkLogin(this);
    },
    /*监听页面显示*/
    onShow: function() {
		this.userId=uni.getSystemInfoSync('userId')
        // #ifdef MP-ALIPAY
        this.isshowSub = false
        // #endif
        this.role = this.$store.state.role;
        this.contactPhone = this.$store.state.userInfo.customerTel || '--';
        this.isManage = this.$store.state.isManage; // 当前用户是否是上货员
    },
    methods: {
		JumpOrder(){
			uni.navigateTo({
			    url: "/pages/mine/order/order"
			})
		},
		JumpCoupon(){
			uni.navigateTo({
			    url: "/pages/mine/coupon/coupon"
			})
		},
		LoginJump() {
		    uni.navigateTo({
		        url: "/subpackages/login/login"
		    })
		},
		activeJump() {
		    uni.navigateTo({
		        url: "/pages/index/active/active"
		    })
		},
		// 点击我的
		mine: function() {
		    uni.navigateTo({
		        url: "/pages/mine/mine"
		    })
		},
		IndexJump(){
			uni.navigateTo({
			    url: "/pages/index/index"
			})
		},
		getBanners() {
		    this.$request(this.$api.getBanners, {
		        deviceId: this.$store.state.deviceId
		    }).then(res => {
		        if (res.code == 200) {
		
		            this.imgUrls = res.result
		        }
		    })
		},
		// 扫码获取数据
		scanToGetData: function() {
		    uni.scanCode({
		        onlyFromCamera: false,
		        success: (res) => {
		            this.$util.extractUri(this, res.result)
		            console.log('扫码')
		            const onLogin = () => {
		                //this.getPageData();
		                this.isManage1();
		                this.getBanners()
		            }
		            this.$util.checkLogin(this, onLogin);
		        },
		        complete: (res) => {
		            console.log(res)
		        }
		    });
		},
		// 报修
		fault: function() {
		    if (this.$store.state.deviceId) {
		        uni.navigateTo({
		            url: "/pages/fault/fault"
		        })
		        return;
		    }
		    uni.showModal({
		        title: '提示',
		        content: '请扫描设备上的二维码，以便我们知道您身边的设备',
		        showCancel: true,
		        cancelText: '取消',
		        confirmText: '扫码',
		        success: res => {
		            if (!res.confirm) {
		                return;
		            }
		            uni.scanCode({
		                onlyFromCamera: false,
		                success: (res) => {
		                    this.$util.extractUri(this, res.result)
		                    //this.getPageData();
		                    this.fault();
		                    this.isManage1();
		                },
		                complete: (res) => {
		                    console.log(res)
		                }
		            });
		        }
		    });
		},	
		// 查询是否上货员
		isManage1: function() {
		    let data = {
		        userId: this.$store.state.userInfo.userId,
		        deviceId: this.$store.state.deviceId,
		    }
		    this.$request(this.$api.IsManage, data, "POST", true, "application/x-www-form-urlencoded").then(res => {
		        let data = JSON.parse(res.data)
		
		        if (data && data.courier == "true") {
		            this.$store.state.isManage = true;
		        } else {
		            this.$store.state.isManage = false;
		        }
		    });
		},
		orderJump(){
			uni.navigateTo({
			    url: "/pages/mine/order/order"
			})
		},
		
        // 开启订阅消息
        openMsg() {
            var that = this
            // 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
            wx.getSetting({
                withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
                success(res) {
                    console.log(res)
                    if (res.authSetting['scope.subscribeMessage']) { //用户点击了“总是保持以上，不再询问”
                        uni.openSetting({ // 打开设置页
                            success(res) {
                                console.log(res.authSetting)
                            }
                        });
                    } else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
                        var templateid = [
                            'jFBgTV5atfDDz2wSSpxzLglRGIPL75tuXsk0iHRbqiU',
                            'xVvYnFKgTiaDbWPOb2wQ95AbuhrNL2RJA9UbheOZr6s',
                            '6GCyt4Tu3Bqv5D9IQkSwMMR0vWsLC9McGj6vy42aPfc'
                        ]
                        uni.requestSubscribeMessage({
                            tmplIds: templateid,
                            success(res) {
                                let successTempid = []
                                for (let i in res) {
                                    if (res[i] == "accept") successTempid.push(i)
                                }
                                const data = {
                                    openId: that.$store.state.userInfo.openId,
                                    userId: that.$store.state.userInfo.userId,
                                    templateIdListStr: successTempid.toString(),
                                }
                                console.log(data)
                                that.$request(that.$api.Subscribe, data, "post").then(res => {})
                                uni.navigateTo({
                                    url: "/pages/mine/redPacket/redPacket"
                                })
                            },
                            fail: (res) => {
                                console.log(res)
                                uni.navigateTo({
                                    url: "/pages/mine/redPacket/redPacket"
                                })
                            }
                        })
                    }
                }
            })
        },
        // 扫码获取绑定信息
        bindManage: function() {
            uni.scanCode({
                onlyFromCamera: false,
                success: (res) => {
                    console.log(res)
                    let paramList = res.result.split("?")[1].split("&");
                    let deviceId = null;
                    let name = null;
                    let userId = this.$store.state.userInfo.userId;
                    paramList.forEach((item, index) => {
                        let key = item.split("=")[0];
                        let value = item.split("=")[1];
                        if (key == "deviceId") {
                            deviceId = value;
                        }
                        if (key == "name") {
                            name = value;
                        }
                    })
                    this.pushBindManage(deviceId, name, userId);
                },
                fail: (res) => {
                    console.log(res)
                },
                complete: (res) => {
                    console.log(res)
                }
            });
        },

        // 绑定上货员
        pushBindManage: function(deviceId, name, userId) {
            if (!deviceId || deviceId == '') {
                uni.showToast({
                    title: '没有设备id',
                    icon: 'none'
                });
                return false;
            }
            if (!name || name == '') {
                uni.showToast({
                    title: '没有名称',
                    icon: 'none'
                });
                return false;
            }
            if (!userId || userId == '') {
                uni.showToast({
                    title: '未登录',
                    icon: 'none'
                });
                return false;
            }
            let data = {
                deviceId,
                name,
                userId,
            }
            this.$util.request(this.$api.BindManage, data, "POST").then(res => {
                if (res.returnCode == "SUCCESS") {
                    this.$store.state.isManage = true;
                    uni.showModal({
                        title: '提示',
                        content: res.returnMessage,
                        showCancel: false,
                    });
                } else {
                    uni.showModal({
                        title: '提示',
                        content: res.returnMessage,
                        showCancel: false,
                    });
                }
            })
        },

        // 联系客服
        phoneContact: function() {
            uni.makePhoneCall({
                phoneNumber: '4008388948', // 电话
            })
        },

        // 显示公众号二维码
        showQrCode: function() {
            this.isShowQrCode = !this.isShowQrCode; // 取反			
        },
        //我的红包
        toMyPacket: function() {
            // #ifdef MP-WEIXIN
            this.openMsg();
            // #endif
            // #ifdef MP-ALIPAY
            uni.navigateTo({
                url: "/pages/mine/redPacket/redPacket"
            })
            // #endif

        },
        // 点击我的订单
        toOrder: function() {
            uni.navigateTo({
                // url: "/pages/mine/order/order"
                url: "/pages/mine/order/newOrder"
            })
        },

        // 点击我的优惠券
        toCoupon: function() {
            uni.navigateTo({
                url: "/pages/mine/coupon/coupon"
            })
        },

        // 点击常见问题
        toQuestion: function() {
            uni.navigateTo({
                url: "/pages/mine/question/question"
            })
        },

        // 点击意见反馈
        toOpinion: function() {
            if (this.$store.state.deviceId) {
                uni.navigateTo({
                    url: "/pages/mine/opinion/opinion"
                })
            } else {
                uni.showModal({
                    title: "提示",
                    content: "请扫描设备上的二维码进入小程序",
                    showCancel: false,
                })
            }
        },

        // 我的余额
        toBalance: function() {
            uni.navigateTo({
                url: "/pages/activity/shiYueWei_prepaid/balance"
            })
        },


        // 切换身份
        switchRole: function() {
            uni.navigateTo({
                url: "/pages/indexManage/switchRole/switchRole"
            })
        },

        // 设备报修
        toRepair: function() {
            uni.navigateTo({
                url: "/pages/fault/fault"
            })
        },
    }
}